<template>
  <el-dialog :visible="isShowAssignRole" title="分配角色" width="40%" @close="onClose">
    <!-- 添加复选框组件，todo 观察文档 -->
    <template>
      <el-checkbox-group v-model="selectedRoleIds">
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>

      </el-checkbox-group>
    </template>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="mini" @click="submit">确定</el-button>
        <el-button type="info" size="mini" @click="onClose">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleEnabled, getUserIdname, putAssignPrem } from '@/api/employee'
export default {
  name: 'AssignRole',
  props: {
    isShowAssignRole: Boolean
  },
  data() {
    return {
      selectedRoleIds: [], // 用户勾选，收集到的角色 id 的集合
      roleList: [],
      userid: null
    }
  },
  methods: {
    async getRoleList(id) {
      this.userid = id
      this.roleList = await getRoleEnabled()
      const res = await getUserIdname(id)
      this.selectedRoleIds = res.roleIds
    },
    onClose() {
      this.roleList = []
      this.$emit('update:is-show-assign-role', false)
    },
    async submit() {
      try {
        const res = await putAssignPrem({
          id: this.userid,
          roleIds: this.selectedRoleIds

        })
        console.log(res)
        this.$message.success('修改成功')
        this.onClose()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
